package com.hsmpay.common.util;

import com.hsmpay.common.util.jiami.Des3;
import org.apache.commons.lang.StringUtils;

public class ClearMac {

    public static String start_suffix = "zhongyi支付";

    public static String user_suffix = "zyzf_userFFFFFFF";

    public static String order_suffix = "zyzf_orderFFFFFF";

    public static String co_suffix = "zyzf_coFFFFFFFFF";

    public static String bcl_suffix = "zyzf_bclFFFFFFFF";

    public static String org_suffix = "zyzf_orgFFFFFFFF";

    public static String bro_suffix = "zyzf_broFFFFFFFF";

    /**
     * 用户修改是 算mac
     *
     * @param loginName
     * @param identityCard
     * @param bankCard
     * @param openingBank
     * @param cardholder
     * @return
     */
    public static String getUserClearMac(String loginName, String identityCard, String bankCard, String openingBank, String cardholder) {
        StringBuffer sb = new StringBuffer();
        if (loginName != null) loginName = loginName.trim();
        if (identityCard != null) identityCard = identityCard.trim();
        if (bankCard != null) bankCard = bankCard.trim();
        if (openingBank != null) openingBank = openingBank.trim();
        if (cardholder != null) cardholder = cardholder.trim();

        sb.append(loginName).append("|").append(identityCard).append("|").append(bankCard).append("|").append(openingBank).append("|").append(cardholder).append("|").append(user_suffix);
        return getMac(sb.toString());
    }

    /**
     * 订单结算 mac
     *
     * @param orderNum
     * @param userId
     * @param transMoney
     * @param bankCard    结算卡号
     * @param openingBank 结算卡开户行
     * @param userName    结算卡持卡人姓名
     * @return
     */
    public static String getOrderClearMac(String orderNum, Long userId, Double transMoney, String bankCard, String openingBank, String userName) {
        StringBuffer sb = new StringBuffer();
        sb.append(orderNum).append("|").append(userId).append("|").append(NumberUtil.numFormat(transMoney, "0.00")).append("|").append(bankCard).append("|").append(openingBank).append("|").append(userName).append("|").append(order_suffix);
        return getMac(sb.toString());
    }

    /**
     * 合单 结算mac
     *
     * @param loginName
     * @param transMoney
     * @param bankCard
     * @param openingBank
     * @param cardholder
     * @return
     */
    public static String getCoClearMac(String loginName, Double transMoney, String bankCard, String openingBank, String userName, Long batchCoId) {
        StringBuffer sb = new StringBuffer();
        sb.append(loginName).append("|").append(NumberUtil.numFormat(transMoney, "0.00")).append("|").append(bankCard).append("|").append(openingBank).append("|").append(userName).append("|").append(batchCoId).append("|").append(co_suffix);
        return getMac(sb.toString());
    }

    /**
     * 结算日志 结算mac
     *
     * @param combineOrderId
     * @param transMoney
     * @param bankCard
     * @param openingBank
     * @param cardholder
     * @return
     */
    public static String getBCLClearMac(Long combineOrderId, Double transMoney, String bankCard, String openingBank, String userName) {
        StringBuffer sb = new StringBuffer();
        sb.append(combineOrderId).append("|").append(NumberUtil.numFormat(transMoney, "0.00")).append("|").append(bankCard).append("|").append(openingBank).append("|").append(userName).append("|").append(bcl_suffix);
        return getMac(sb.toString());
    }

    /**
     * 机构 结算mac
     *
     * @param broStatus
     * @param bankCard
     * @param openingBank
     * @param cardholder
     * @return
     */
    public static String getOrgClearMac(Long broStatus, String bankCard, String openingBank, String cardholder) {
        StringBuffer sb = new StringBuffer();
        sb.append(broStatus).append("|").append(bankCard).append("|").append(openingBank).append("|").append(cardholder).append("|").append(org_suffix);
        return getMac(sb.toString());
    }

    /**
     * 佣金订单 结算mac
     *
     * @param organizationId
     * @param bankCard
     * @param openingBank
     * @param cardholder
     * @return
     */
    public static String getBroClearMac(Long organizationId, Double totalBrokerage, Double transMoney, String bankCard, String openingBank, String userName) {
        StringBuffer sb = new StringBuffer();
        sb.append(organizationId).append("|").append(NumberUtil.numFormat(totalBrokerage, "0.00")).append("|").append(NumberUtil.numFormat(transMoney, "0.00")).append("|").append(bankCard).append("|").append(openingBank).append("|").append(userName).append("|").append(bro_suffix);
        return getMac(sb.toString());
    }

    /**
     * 计算 mac
     * 使用 正规MD5 加密
     *
     * @param str
     * @return
     */
    private static String getMac(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        String end_suffix = str.substring(str.lastIndexOf("|") + 1);
        String Key = start_suffix + end_suffix;
        return end_suffix + Des3.encode(str, Key);
//		return Des3.encode(str);
    }

    /**
     * 解密
     *
     * @param str
     * @param end_suffix
     * @return
     */
    public static String dencodeMac(String str) {
        try {
            String end_suffix = str.substring(0, 16);
            //System.out.println("end_suffix = "+end_suffix);
            String Key = start_suffix + end_suffix;
            //System.out.println("Key = "+Key);
            String eStr = str.substring(16);
            //System.out.println("eStr = "+eStr);
            return Des3.decode(eStr, Key);
//			return Des3.decode(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {

//		System.out.println(dencodeMac("zyzf_userFFFFFFFDwaAoPMCPZOauRwW/cHsB4gon0YA1SIndE1VzQRDs4tjnVpjZUEZlAja4vk3qPpGHGvGtvdVwPjj3CjTQP6TI2Eh6BgYJt2ooiOLPEHC+F1H2/rs+diWeE/eM8+cWr7D"));
//		
//		
//		System.out.println(dencodeMac("zyzf_userFFFFFFFNpwox+PKWXSxzwCYWSDvNz+ZoLXey4gqk4N1Y8tWp0+33obF1RRXUaQ/Qnbm0t4MaTWCxUx7EZUvocGnVYRELr5W8/WUBnL1bSGWA2fDyH14PcbyNQflObUs+076aV2l"));
//		System.out.println(dencodeMac("zyzf_userFFFFFFF9ZTVKESaOzV2KXQbVIxIN/RniRUBhq9KWGDb7Fa/G52u+svt4X1M08FNW/iMvXNKVP6IoT2JkTZK/0NxWiNb4ZKyWRmq4PEUCxzOQDoni0qvatsLPSLT9+Ry3cdq4zDE"));
//		System.out.println(dencodeMac("zyzf_userFFFFFFFXjqiU5JtQ0R59iWo8CdGd3VxRWfpRRH8yEs6dP3yIx0hNNb06YfVhVmQJhmH7GqKGMi4tGu8B4iD0GKlzDnm67qa/3IybL7JLrJG1NRNCDhvIobg+v8pLeuCnhwg7864"));
//		
//		System.out.println(dencodeMac("zyzf_userFFFFFFFfMBPFjICw1nRRTHoPT5W2tsJQf+mX8pznjGwjZwx2I0sctBq+uLWjE7iTVdmfif5OLeK9Bb36lkSP77NHDZF/V6nZlRAl4MFoBid9KDAX9jPtld10AvERG+8CgeolrHY"));
//		
//		
//		System.out.println("   shh   sdf  ".trim());
//		
//		System.out.println(dencodeMac("zyzf_userFFFFFFFgLxaSHog+Tngd+kxx/koBUv90UBQF3BnCszcK1cpuCekKve/sm4luHs9pMb0vK+Cwwmt1FZ9ut0OAWqa+Luzrap/p3GluqUhV8adDIxhzbQYdnlT4kIbQw=="));
//		System.out.println(dencodeMac("zyzf_userFFFFFFFes8Vs/eXSfMhT6wMNlMVdfKV38nvJPLgcAfi7Ii6E/ZqI0/DlrEZywCD9xREbknE2Yfh6H908LyLWc6m2V96DPqxemUoSWEzJl2o/6L5ZzEYcjAKyJu3vud14xgonJM5"));
//		System.out.println(dencodeMac("zyzf_userFFFFFFF2g5ACRvbsKmNiTKCELmVhabcLKYvENMUsPFeJtb62Diz4y6wEgR0ci43LsvO+WZIVyNMOfe9KBp6vSROqtNrX3qjIvl249CkLC0FzMPrLdSTB14LsdT9Fw=="));
//		System.out.println(dencodeMac("zyzf_userFFFFFFFmpup9+7ebb1glfe0k2//vUz85t6DZwjXjMx7R2fy0YI4j+Q8xlyQRF6pwlxwcOtTG58HleaF7PwS7smrZkaCL0a5r/hwurd8cf9wRCUp0I6xpi1197RRCcsfRSQw0vcJRe23/tX00qc="));
//		System.out.println(dencodeMac("zyzf_userFFFFFFFtnNqr2ZMJt3XfuJpKptSRHLAGiFf2Dw2fhF5sW8o9fe28sKxO7W3Z2+ZyZ7a8eB7DkLIRdzMHMuiZnJP0yuk8Zm4oav0JTrn5tq0VFwrfb24i538zn6ekamAL09/no111NfxSWlAY/I="));
//		
//		System.out.println(dencodeMac("zyzf_userFFFFFFFfMBPFjICw1nRRTHoPT5W2tsJQf+mX8pzAE90Prps6v5Vs50TqRRroEs4gFxoRJzFTzhrVzqtFFut4d9UvrzQw2jWMr/EsjKzgs03muONtaDAsgoK+tjaTwZQbUKNvi//"));
//		
//		System.out.println(getUserClearMac("63F2E263", "342623197401275523", "6222600910051345641", "交通银行", "张慧"));

//		System.out.println(getUserClearMac("270265B4", "310111196911170428", "6228480030715678915", "中国农业银行", "陈美珍"));
//		System.out.println(getUserClearMac("3962067B", "130633198611120032", "6222080409002257491", "中国工商银行", "梁旭"));
//		System.out.println(getUserClearMac("3AE01620", "350583198510136704", "6222021001097222332", "中国工商银行", "谢秋燕"));
//		System.out.println(getUserClearMac("1504835F", "46002219801207071X", "6236683320002999324", "中国建设银行", "林方秀"));
//		System.out.println(getUserClearMac("DED1BDB7", "440301198205221917", "6212264000030444182", "中国工商银行", "蔡健明"));
//		System.out.println(getUserClearMac("27CD1CD7", "341122198007251216", "6225880175488106", "招商银行", "梁月"));
//		System.out.println(getUserClearMac("18219315086", "430422198309200059", "6214921201784127", "中国光大银行", "颜亚辉"));
//		System.out.println(getUserClearMac("18343367666", "222426197803300323", "6227000901450024726", "中国建设银行", "姜艳"));
//		System.out.println(getUserClearMac("13288847502", "440104199402151615", "6212262112000581240", "中国工商银行", "高耀鸿"));

//		//为变为3des加密的用户
//		System.out.println(getUserClearMac("13808707003", "360313198412160016", "6236683860000984919", "中国建设银行", "杨宁"));
//		System.out.println(getUserClearMac("18003578315", "142625198310053319", "6212260510001796794", "中国工商银行", "贾志安"));
//		System.out.println(getUserClearMac("13819373888", "330501198102210049", "6222021204012063092", "中国工商银行", "戴亚纳"));
//		System.out.println(getUserClearMac("13799788913", "350212198605163527", "6236681930000538700", "中国建设银行", "吕惠娟"));
//		System.out.println(getUserClearMac("15729531980", "642127198107213632", "6217998700002278946", "中国邮政储蓄银行", "马黑牙"));
//		System.out.println(getUserClearMac("15312699971", "330381198809154715", "6228480322501310716", "中国农业银行", "黄忆余"));
//		System.out.println(getUserClearMac("13259870585", "370784198007211819", "6217855000018021493", "中国银行", "郑宗杰"));


//		System.out.println(getUserClearMac("18183393375", "510523196612183477", "6228482108566344874", "中国农业银行", "周建友"));
//		System.out.println(getUserClearMac("15010010590", "132326197407190436", "6226621001525227", "中国光大银行", "李西坤"));
//		System.out.println(getUserClearMac("15093335573", "410183198908100060", "6228480718853288774", "中国农业银行", "张臻"));
//		System.out.println(getUserClearMac("13258573678", "232330197611060629", "6217230912000453078", "中国工商银行", "李亚凤"));
//		System.out.println(getUserClearMac("18698735378", "210202198101114226", "6217000780010075712", "中国建设银行", "解慧慧"));
        //3?	68986	18714513515	6214854512025122	220112198406060010		684F632E20A3CE2868E3CCD98F91A4CE		1	1	24-5月 -15 10.32.19.000000 上午	1		3	招商银行	0	2	哈尔滨分行	8	2610		乔磊	851		FL2NGAYLQYE364EC5F	308261032013	1		10007	0.00400	0.00400	1.0000		0.00	0.00	32	0.00200				4		zyzf_userFFFFFFFPtADPP/BzI5U4eKtqIu1RVZ8914YEAEFPMV5hyH6qv/u2bdi5zMHQDzbKuFnxupko/yD16boMK24z2gvAnGjjVnD/5ElcUOCJSJUVSAT6vG7E1wm7KQkoUW4kmmBtjx6	1		3	AAAWDcAAGAADfVBAAH
//		System.out.println(getUserClearMac("18714513515", "220112198406060010", "6214854512025122", "招商银行", "乔磊"));


//		System.out.println(getUserClearMac("406C057C", "330482199111080917", "6228480348982779475", "中国农业银行", "杨忠伟"));
//		System.out.println(getUserClearMac("15004326321", "220211199003270332", "6222020802003413423", "中国工商银行", "高显鹏"));
//		System.out.println("CCB4F204DEC087C9DFC136EDC393C964");
//		System.out.println(getCoClearMac("13959861585", 49.75, "6228410680079057612", "中国农业银行", "林増加", Long.valueOf(291561)));
//		
//		
//		System.out.println("50C305D83F6CA80AE48AF47B110D8380");
//		System.out.println(getOrderClearMac("2015011823000000000470828938", Long.valueOf(47082), 19712.81+"", null, null, "沈剑"));
//		
//		
//		System.out.println("C90DCB9083DAADDC109B29AE49381070");
//		System.out.println(getUserClearMac("6D27D3B0", "610403199102101036", "6228480211052097311", "中国农业银行", "党欣"));
//		
//		
////		System.out.println("3836CE1F09204AD09DDB1884AE1AE54F");
////		System.out.println(getUserClearMac("174C0D8B", "130925199102155131", "62284841722585223", "中国建设银行", "张三"));
//		
//		System.out.println("36E6E6FDA2AADA3FEAF3D3C5C96131D2");
//		System.out.println(getUserClearMac("1D1547A0", "441224198805163773", "6227003110512265247", "中国建设银行", "林志盟"));

//		String mac = getUserClearMac("15768021", "142701198812034817", "6222020508003014281", "中国工商银行", "刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭刘拴庭");
//		System.out.println(mac);
//		System.out.println(dencodeMac(mac));


//		System.out.println(mac.length());
//		System.out.println("11212|1212|zyzf".substring("11212|1212|zyzf".lastIndexOf("|")+1));

        //31134 15040396009	340822198004070214	6214850241954368	招商银行	林国兵
        //55712	15776394127	232101198903264617	6217001140011360200	中国建设银行	王克维
        //57076	15129870110	612622198111020112	6217004180001251719	中国建设银行	呼永涛
        //58153	13228158688	510104197804061670	6228450460024858310	中国农业银行	郑成
//		String mi = getUserClearMac("15040396009", "340822198004070214", "6214850241954368", "招商银行", "林国兵");
//		System.out.println("31134 ："+mi);
//		System.out.println("55712 ："+getUserClearMac("15776394127", "232101198903264617", "6217001140011360200", "中国建设银行", "王克维"));
//		System.out.println("57076 ："+getUserClearMac("15129870110", "612622198111020112", "6217004180001251719", "中国建设银行", "呼永涛"));
//		System.out.println("58153 ："+getUserClearMac("13228158688", "510104197804061670", "6228450460024858310", "中国建设银行", "呼永涛"));
//		System.out.println(dencodeMac(mi));
//		System.out.println(getBCLClearMac(692867L, 18023.92, "6210810940005207713", "中国建设银行", "黄广志"));
        //13521856049|430923198911167515|6226220112722482|中国民生银行|颜铃璋
//		System.out.println(getUserClearMac("13521856049", "430923198911167515", "6226220112722482", "中国民生银行", "颜铃璋"));
//		System.out.println(dencodeMac("zyzf_userFFFFFFFcbUGejs9If3GPAnq++W/W5HnvGoRhBjtT6X4oqbwKaiHp+Pz5y0J/GyHc7bhgCvOko/+gtfkCpl+l4Jj7/zieRYiBN/HsLbDgvxMTOtXrrQcoerwDsJ0DdqZhotmRX07"));
        System.out.println(dencodeMac("zyzf_userFFFFFFFH4VbB+9Htf9pK3YMKCd/aX/abt7wKr4Zc4K3Mj0/IepaYuyxUiZMVBLPBOolmd760gmBTRFd4tc4Bz52KpWSpSlx2tIL2shtyWhdeOlK++eHGY5cWlRIOLcp1NtnVhPdos9S2RzDUfI="));
        Double a = 116.194;

        System.out.println(NumberUtil.numFormat(a, "0.00"));
    }
}

